﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace YouYi.YingSheng.Core
{
    public class DapperHelper<T>
    {
        #region 数据连接字符串
        /// <summary>
        /// 数据连接字符串
        /// </summary>
        private static string connString = ConfigurationManager.AppSettings["connString"];
        #endregion

        #region 查询第一个数据
        /// <summary>
        /// 查询第一个数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static T QueryFirst(string sql, object param)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                return conn.QueryFirst(sql, param);
            }
        }
        #endregion

        #region 查询第一个数据没有返回默认值
        /// <summary>
        /// 查询第一个数据没有返回默认值
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static T QueryFirstOrDefault(string sql, object param)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                return conn.QueryFirstOrDefault(sql, param);
            }
        }
        #endregion

        #region 查询单条数据
        /// <summary>
        /// 查询单条数据
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static T QuerySingle(string sql, object param)
        {
            using (SqlConnection con = new SqlConnection(connString))
            {
                return con.QuerySingle<T>(sql, param);
            }
        }
        #endregion

        #region 查询单条数据没有返回默认值
        /// <summary>
        /// 查询单条数据没有返回默认值
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static T QuerySingleOrDefault(string sql, object param)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                return conn.QuerySingleOrDefault<T>(sql, param);
            }
        }
        #endregion

        #region 查询返回一个表
        /// <summary>
        /// 查询返回一个表
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static List<T> QueryList(string sql, object param)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                return conn.Query<T>(sql, param).ToList();
            }
        }
        #endregion

        #region 查询object对象
        /// <summary>
        /// 查询object对象
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static object QueryScalar(string sql, object param)
        {
            using (SqlConnection conn = new SqlConnection(connString))
            {
                return conn.ExecuteScalar(sql, param);
            }
        }
        #endregion

        #region 增删改
        /// <summary>
        /// 增删改
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static int Execute(string sql, object param)
        {
            using (SqlConnection con = new SqlConnection(connString))
            {
                return con.Execute(sql, param);
            }
        }
        #endregion

        #region 带参数的存储过程
        /// <summary>
        /// 带参数的存储过程
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public static List<T> ExecutePro(string sql, object param)
        {
            using (SqlConnection con = new SqlConnection(connString))
            {
                List<T> list = con.Query<T>(sql,
                    param,
                    null,
                    true,
                    null,
                    CommandType.StoredProcedure).ToList();
                return list;
            }
        }
        #endregion

        #region 事务-全SQL
        /// <summary>
        /// 事务
        /// </summary>
        /// <param name="sqlarr"></param>
        /// <returns></returns>
        public static int ExecuteTransaction(string[] sqlarr)
        {
            using (SqlConnection con = new SqlConnection(connString))
            {
                //con.Open();
                //开启事务
                using (var transaction = con.BeginTransaction())
                {
                    try
                    {
                        int result = 0;
                        foreach (var sql in sqlarr)
                        {
                            result += con.Execute(sql, null, transaction);
                        }

                        //提交事务
                        transaction.Commit();
                        return result;
                    }
                    catch (Exception ex)
                    {
                        //回滚事务
                        transaction.Rollback();
                        return 0;
                    }
                }
            }
        }
        #endregion

        #region 事务带参数
        /// <summary>
        /// 事务带参数
        /// </summary>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static int ExecuteTransaction(Dictionary<string, object> dic)
        {
            using (SqlConnection con = new SqlConnection(connString))
            {
                using (var transaction = con.BeginTransaction())
                {
                    try
                    {
                        int result = 0;
                        foreach (var sql in dic)
                        {
                            result += con.Execute(sql.Key, sql.Value, transaction);
                        }

                        transaction.Commit();
                        return result;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        return 0;
                    }
                }
            }
        }
        #endregion

        #region 单表的批量操作
        /// <summary>
        /// 单表的批量操作
        /// </summary>
        /// <param name="sqlarr"></param>
        /// <returns></returns>
        public static int BatchExecute(string sql, List<T> list)
        {
            using (SqlConnection con = new SqlConnection(connString))
            {
                //开启事务
                using (var transaction = con.BeginTransaction())
                {
                    try
                    {
                        int result = 0;
                        result = con.Execute(sql, list, transaction);
                        //提交事务
                        transaction.Commit();
                        return result;
                    }
                    catch (Exception ex)
                    {
                        //回滚事务
                        transaction.Rollback();
                        return 0;
                    }
                }
            }
        }
        #endregion
    }
}
